package com.gift.service.Impl;

import com.gift.common.PaGing;
import com.gift.common.ServerResponse;
import com.gift.dao.GamesMapper;
import com.gift.pojo.Games;
import com.gift.service.GamesService;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;


import javax.annotation.Resource;
import java.util.List;

/**
 * Created by Administrator on 2017/12/3.
 */
@Service("gamesService")
public class GamesServiceImpl implements GamesService {
    @Resource
    GamesMapper gamesMapper;

//得到全部游戏
    @Override
    public PaGing getAllBypr(int page, int rows) {

        //select * from tableName where 条件 limit 当前页码*页面容量-1,页面容量
        int raa = (page-1) * rows;
        int total =gamesMapper.getCountAll();
        List<Games> gamesList= gamesMapper.getAllBypr(raa,rows);
        PaGing paGing= new PaGing(total,gamesList);
        return paGing;
    }
//新增一个游戏
    @Override
    public ServerResponse insetNewGame(Games newOneGames) {

        int res = gamesMapper.insert(newOneGames);
        if(res != 1){
            return ServerResponse.createByErrorMessage("新增游戏失败");
        }
        return ServerResponse.createBySuccessMessage("新增游戏成功");
    }
//修改一个游戏
    @Override
    public ServerResponse updataGame(Games alterGames) {

        int res = gamesMapper.updateByPrimaryKey(alterGames);
        if(res != 1){
            return ServerResponse.createByErrorMessage("修改游戏失败");
        }
        return ServerResponse.createBySuccessMessage("修改游戏成功");

    }

    @Override
    @Transactional(propagation = Propagation.REQUIRES_NEW)
    public ServerResponse deleteGidList(List<Integer> deleteIdList) {
        int res = gamesMapper.deleteByPrimaryKeyList(deleteIdList);
        if(res<=0){
            return ServerResponse.createByErrorMessage("删除游戏失败");
        }
        return ServerResponse.createBySuccessMessage("删除游戏成功");
    }

    @Override
    public List<Games> getAll() {
        return gamesMapper.getAll();
    }
}
